package com.javademo.threadpool;


import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

@Slf4j
public class ThreadPoolTest1 {
    public static void main(String[] args) {

        int corePoolSize = 2;
        int maximunPoolSize = 4;
        long keepALiveTime = 10;
        TimeUnit timeUnit = TimeUnit.SECONDS;
        BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(2);
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();

        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
                corePoolSize,
                maximunPoolSize,
                keepALiveTime,
                timeUnit,
                queue,
                threadFactory,
                rejectedExecutionHandler
        );

        for (int i = 0; i < 10; i++){
            final int taskNum = i;
            threadPool.execute(() -> {
                System.out.println("Task " + taskNum + " is running!");
                try {
                    Thread.sleep(5000);
                }catch (InterruptedException e){
                    log.info(e.getMessage());
                }
            });
        }

        threadPool.shutdown();
    }
}
